/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	hey 
 * 
 * <p>描   述  :   BridgeDetectionController.java for com.pengtu.gsj.controller.checker.yh.bridgeTunnel    </p>
 * 
 * <p>最后修改 : $: Sep 18, 2017-9:06:40 AM v 1.0.0	 hey   $ </p>
 * 
*/

package com.pengtu.gsj.controller.checker.yh.bridgeTunnel;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.CheckTableBaseController;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.check.yh.bridgeTunnel.BridgeDetectionService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * BridgeDetectionController
 * 
 * Sep 18, 2017 9:06:40 AM
 * 
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("bridgeDetection")
public class BridgeDetectionController extends CheckTableBaseController {
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private BridgeDetectionService bridgeDetectionService;
	@Autowired
	private DownService downService;
	
	/**
	 * 
	 * goToPage:
	 * 适用:跳转页面
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("show")
	@Token(save = true)
	public String goToPage(Model model) {
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		return "check/yh/bridgeTunnel/bridgeDetecition";
	}
	
	/**
	 * 
	 * getSearchMap:
	 * 适用:查询，现场检查，默认查询
	 * @param checkTable
	 * @param checkProject
	 * @param sysmenu
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "search", produces = "application/json; charset=utf-8")
	public Map<String, Object> getSearchMap(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		//返回数据和业务文件，以及得分情况
		map = bridgeDetectionService.getDevelopPlanmMap(checkProject.getStartTime(), checkProject.getEndTime(), checkProject.getCountryOrProvince(), checkTable, sysmenu == null ? "" : sysmenu.getTag());
		//附件查询
		return map;
		
	}
	
	/**
	 * 保存
	 * save:
	 * 适用:
	 * @param checkTable
	 * @param checkProject
	 * @param attributes
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("save")
	@Token(remove = true)
	public String save(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, RedirectAttributes attributes){
		checkTable.setCheckProject(checkProject);
		double tableFull =0;
		if ("0".equals(checkProject.getCountryOrProvince())) {
			tableFull = checkTable.getYearFull()*2;
		} else if ("1".equals(checkProject.getCountryOrProvince())) {
			tableFull = checkTable.getYearFull();
		}
		checkTable.setTableFull(MathsUtils.formatDouble(tableFull, 2)); //设置当前检查表格的总分
		checkTableService.save(checkTable);
		attributesParams(checkTable, attributes);
		return "redirect:/bridgeDetection/show.do";
	}
	
	/**
	 * 
	 * downWordAndZip:
	 * 适用:下载桥梁监测整改表内容
	 * @param checkProject
	 * @param checkTable
	 * @param sysmenu
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("downWord")
	public ResponseEntity<byte[]> downWordAndZip(@ModelAttribute CheckProject checkProject, @ModelAttribute CheckTable checkTable, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String sysName = sysmenu == null ? "" : sysmenu.getName();
		//根据搜索条件查询出来的相关结果
		resultMap = bridgeDetectionService.getDevelopPlanmMap(checkProject.getStartTime(), checkProject.getEndTime(), checkProject.getCountryOrProvince(), checkTable, sysmenu == null ? "" : sysmenu.getTag());
		//将数据记录关联的文件添加到list集合中 在service中一起压缩
		return downService.downWordAndZip(SpringMvcUtils.getParameter("checkSubmoduleName"), SpringMvcUtils.getParameter("checkModuleName"), sysName, "maintenance/桥梁监测整改.xml", resultMap);
	}
}
